//
//  URLViewController.m
//  HYHOME
//
//  Created by Mac on 16/1/22.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "URLViewController.h"
#import "Modelcontent.h"
#import "DataBaseHandle.h"
#import "Like.h"
#import "DKNightVersion.h"
@interface URLViewController ()<UIWebViewDelegate>

@property (nonatomic,retain) UIButton *button;
@property (nonatomic,assign) BOOL isSave;

@end

@implementation URLViewController

- (void)viewDidAppear:(BOOL)animated{

    
    
    [super viewWillAppear:animated];
    
    self.array = [NSMutableArray array];
    self.array = [[DataBaseHandle shareDataBaseHandle] select].mutableCopy;
    

    for (Modelcontent *model in self.array) {
        if ([model.title isEqualToString:self.model.title]) {
            
            [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucangyishoucang"] forState:UIControlStateNormal];
            self.isSave = YES;
            
            break;
            
        }
        else {
            
            [self.button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            
            [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucangweishoucang"] forState:UIControlStateNormal];
            
            
            self.isSave = NO;
            
        }
    }


}

- (void)viewDidLoad {
    [super viewDidLoad];

    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ISNIGHT"]) {
        [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#444444'"];
        [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='#F5FFFA'"];
        [self.webView reload];

    }else {
        [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#ffffff'"];
      
        [self.webView reload];
    }
    

    
}





- (void)passSecondModel:(Modelcontent *)model{
    
    self.model = model;
    
       CGRect frame = self.view.frame;
    self.webView = [[UIWebView alloc ]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
    self.webView.userInteractionEnabled =YES;
    self.webView.delegate = self;
    self.webView.opaque = NO;
    [self.view addSubview:self.webView];
    

    NSURL *url = [NSURL URLWithString:model.content_url];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
 
    [self.webView reload];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    self.button.frame = CGRectMake(10, 10, 10, 10);
    
    [self.webView addSubview:self.button];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] init];
    
    self.navigationItem.rightBarButtonItem.customView = [[UIView alloc] init];
    
    self.navigationItem.rightBarButtonItem.customView.frame = CGRectMake(40, 40, 25, 25);
    [self createButton];
  

}


-(void) createButton {
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, self.navigationItem.rightBarButtonItem.customView.frame.size.width, self.navigationItem.rightBarButtonItem.customView.frame.size.height);
    [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucangweishoucang"] forState:UIControlStateNormal];
    
    [self.navigationItem.rightBarButtonItem.customView addSubview:self.button];
    
    [self.button addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)addAction:(UIButton *)button {
    
    if (self.isSave == NO) {
        
        [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucangyishoucang"] forState:UIControlStateNormal];
        NSLog(@"%@", self.model.title);
        [[DataBaseHandle shareDataBaseHandle] insertStudent:self.model];
        
        
        self.isSave = YES;
    }
    
    else {
        
        
        [[DataBaseHandle shareDataBaseHandle] deleteWithModel:self.model];
        
        
        [self.button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        
        [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucangweishoucang"] forState:UIControlStateNormal];
        
        self.isSave = NO;
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)webViewDidStartLoad:(UIWebView *)webView{
//
//    [self.webView reload];
//
//}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
   
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ISNIGHT"]) {
                [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#444444'"];
        

        [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='#7D7D7D'"];

        
    }else {
          [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#ffffff'"];


    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
